package cn.itcast.hotel.listener;

import cn.itcast.hotel.service.IHotelService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HotelListener {
    @Autowired
    private IHotelService hotelService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("hotel.insert.queue"),
            exchange = @Exchange(name = "hotel.topic",type = ExchangeTypes.TOPIC),
            key = {"hotel.insert"}
    ))
    public void listenToInsert(Long id){
        if (id!=null){
            hotelService.insert(id);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("hotel.delete.queue"),
            exchange = @Exchange(name = "hotel.topic",type = ExchangeTypes.TOPIC),
            key = {"hotel.delete"}
    ))
    public void listenToDelete(Long id){
        if (id!=null){
            hotelService.delete(id);
        }
    }
}
